package com.example.anew.journey.mynewjourney.logic.PasswordFreeLogin;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.example.anew.journey.mynewjourney.base.BasePasswordFreeLoginAndRegisteredActivity;
import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.FirstChangeMainActivity;
import com.example.anew.journey.mynewjourney.logic.getVersion.GetVersionActivity;
import com.example.anew.journey.mynewjourney.logic.getVersion.bean.GetVersionBean;
import com.example.anew.journey.mynewjourney.logic.login.LoginActivity;
import com.example.anew.journey.mynewjourney.logic.setName.SetNameActivity;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司14
 * Created by 20433 on 2018/3/6
 */

public class PasswordFreeLoginActivity extends BasePasswordFreeLoginAndRegisteredActivity {
    /**
     * @desc 根据获取到的手机号跳转到获取验证码页面
     */
    @Override
    protected void toGetVersionCode(String trim) {
        if (TextUtils.isEmpty(trim)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }

        if(third_party_login_state != null){
            if(third_party_login_state.equals("1")){
                Intent intent =  new Intent(PasswordFreeLoginActivity.this,SetNameActivity.class);
                intent.putExtra("phone",trim);
                intent.putExtra("third_party_login_state",third_party_login_state);
                intent.putExtra("third_party_login_id",third_party_login_id);
                intent.putExtra("third_party_login_name",third_party_login_name);
                startActivity(intent);
            }
        }else{
            Intent intent =  new Intent(PasswordFreeLoginActivity.this,GetVersionActivity.class);
            intent.putExtra("phone",trim);
            intent.putExtra("my_login_state",1);
            startActivity(intent);
            loadVersionCode(trim);
        }

    }

    private void loadVersionCode(String trim) {
        String url = RBConstants.SERVER_URL+RBConstants.OTHER_GET_VERSION_CODE;
        OkHttpUtils
                .get()
                .url(url)
                .addParams("tel", trim)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson =   new Gson();
                        //NLogger.e("TAG","=====获取免密码登陆验证码====NetworkResponseDate============"+NetworkResponseDate);
                        final GetVersionBean getVersion =  gson.fromJson(NetworkResponseDate,GetVersionBean.class);
                        // TODO 移植
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(getVersion.errorCode == 200) {

                                    Toast.makeText(PasswordFreeLoginActivity.this,"验证码发送成功",Toast.LENGTH_SHORT).show();

                                }else{
//                                    Intent intent = new Intent(PasswordFreeLoginActivity.this, RegisteredOrLoginActivity.class);
//                                    mContext.startActivity(intent);
//                                    finish();
                                    Toast.makeText(PasswordFreeLoginActivity.this,getVersion.errorMessage,Toast.LENGTH_SHORT).show();

                                }

                            }
                        });


                        return null;
                    }
                    @Override
                    public void onError(Call call, Exception e, int id) {

                        Toast.makeText(PasswordFreeLoginActivity.this,"验证码发送失败，请重新发送",Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }

    /**
     * @desc 初始化数据
     */
    @Override
    protected void initDate() {
        displayTvAgreement();
    }

    @Override
    protected void toMainPager() {
        Intent intent =  new Intent(PasswordFreeLoginActivity.this,FirstChangeMainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);

    }

    /**
     * @desc 点击发布按钮
     */
    protected void onClickRelease() {
        Intent intent = new Intent(PasswordFreeLoginActivity.this, LoginActivity.class);
        //intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        startActivity(intent);

    }
}
